package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckGroupDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

/**
 * @author 林道样
 * @date 2020-02-21 18:39
 */
@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    CheckGroupDao checkGroupDao;

    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {
        checkGroupDao.edit(checkGroup);
        checkGroupDao.delItemAndGroup(checkGroup.getId());
        addGAndItem(checkGroup.getId(), checkitemIds);
    }

    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        checkGroupDao.addGroup(checkGroup);
        addGAndItem(checkGroup.getId(), checkitemIds);
    }

    @Override
    public void delect(Integer id) {
        checkGroupDao.delItemAndGroup(id);
        checkGroupDao.delGroup(id);
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        String queryString = queryPageBean.getQueryString();
        Page<CheckGroup> page = checkGroupDao.findPage(queryString);
        long total = page.getTotal();
        return new PageResult(total,page);
    }

    @Override
    public CheckGroup findById(Integer id) {
        CheckGroup page =checkGroupDao.findById(id);
        return page;
    }

    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
       List<Integer> list= checkGroupDao.findCheckItemIdsByCheckGroupId(id);
        return list;
    }

    @Override
    public List<CheckGroup> findAll() {
        List<CheckGroup> list = checkGroupDao.findAll();
        return list;
    }

    public void addGAndItem(Integer checkGroupId, Integer[] checkitemIds) {

        for (Integer checkitemId : checkitemIds) {
            HashMap<String, Integer> map = new HashMap<>();
            map.put("checkGroupId", checkGroupId);
            map.put("checkItemId", checkitemId);
            checkGroupDao.addItemAndGroup(map);
        }
    }

}
